package com.example.su_shi_tan_mine

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.GridView
import android.widget.SimpleAdapter
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.adapter.ab_list_adapter.CommonAdapter
import com.example.su_shi_tan_bas_lib.adapter.ab_list_adapter.CommonViewHolder
import com.example.su_shi_tan_bas_lib.album.view.grid_view.adapter.CommAdapter
import com.example.su_shi_tan_bas_lib.album.view.grid_view.adapter.XQuickViewHolder
import com.example.su_shi_tan_bas_lib.base.BaseDataBindingFragment
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment

import com.example.su_shi_tan_bas_lib.utils.even.MessageWrap
import com.example.su_shi_tan_bas_lib.utils.even.TO_REFRESH_USER_INFO
import com.example.su_shi_tan_bas_lib.view.LabelGridView
import com.example.su_shi_tan_core.arouter.MineARouter
import com.example.su_shi_tan_core.arouter.PAGE_INDEX_OF
import com.example.su_shi_tan_core.db.LogInTokenManager
import com.example.su_shi_tan_core.db.UserInfoBean
import com.example.su_shi_tan_core.db.UserInfoBeanHolder
import com.example.su_shi_tan_core.extentions.startNavigation
import com.example.su_shi_tan_core.getUserInfo
import com.example.su_shi_tan_core.utils.clipboard
import com.example.su_shi_tan_mine.arouter.SubPageARouter
import com.example.su_shi_tan_mine.config.*
import com.example.su_shi_tan_mine.config.SERVICE_CONFIG_LIST

import com.example.su_shi_tan_mine.databinding.MineFragmentBinding

@Route(path = MineARouter.MineFragment)
class MineFragment : BaseLazyDataBindingFragment<MineFragmentBinding>(), View.OnClickListener,
    AdapterView.OnItemClickListener {

    override fun layoutId() = R.layout.mine_fragment


    private var mGridViewAdapter: CommonAdapter<ConfigBean>? = null


    private var orderGridViewAdapter: CommonAdapter<OrderConfigBean>? = null

    private var mServiceAdapter: CommonAdapter<ConfigBean>? = null

    override fun initView(view: View) {
        viewBinder.onViewClick = this
        orderGridViewAdapter = object : CommonAdapter<OrderConfigBean>(
            _mActivity,
            ORDER_CONFIG_LIST,
            R.layout.mine_order_fragment
        ) {
            override fun convert(helper: CommonViewHolder?, item: OrderConfigBean?, position: Int) {
                helper?.setText(R.id.title_template, item?.title ?: 0)
                    ?.setImageResource(R.id.icon, item?.icon ?: 0)
            }
        }
        viewBinder.orderGridView.adapter = orderGridViewAdapter
        viewBinder.orderGridView.onItemClickListener = this


        mGridViewAdapter =
            object : CommonAdapter<ConfigBean>(
                _mActivity,
                SERVICE_CONFIG_LIST,
                R.layout.mine_service_fragment
            ) {
                override fun convert(helper: CommonViewHolder?, item: ConfigBean?, position: Int) {
                    helper?.setText(R.id.title_template, item?.title ?: 0)
                        ?.setImageResource(R.id.icon, item?.icon ?: 0)
                }

            }
        viewBinder.mIntegratedApplication.adapter = mGridViewAdapter

        viewBinder.mIntegratedApplication.onItemClickListener = this


        mServiceAdapter = object : CommonAdapter<ConfigBean>(
            _mActivity,
            MY_SERVICE,
            R.layout.mine_service_fragment
        ) {
            override fun convert(helper: CommonViewHolder?, item: ConfigBean?, position: Int) {
                helper?.setText(R.id.title_template, item?.title ?: 0)
                    ?.setImageResource(R.id.icon, item?.icon ?: 0)
            }

        }
        viewBinder.mService.adapter = mServiceAdapter
        viewBinder.mService.onItemClickListener = this
    }


    override fun lazyInit() {
        if (LogInTokenManager.tokenManager.haveSignedIn()) {
            viewBinder.userInfo = UserInfoBeanHolder.getUserInfoBean()
        }
    }

    override fun refreshData() {

    }

    override fun handMessage(msg: MessageWrap?) {
//        super.handMessage(msg)
        when (msg?.message) {
            TO_REFRESH_USER_INFO -> {
                viewBinder.userInfo = UserInfoBeanHolder.getUserInfoBean()

            }
        }
    }

    override fun onClick(v: View?) {

        when (v?.id) {
            R.id.ic_copy_icon -> {
                val userInfo = UserInfoBeanHolder.getUserInfoBean()
                clipboard("", (userInfo?.invite_code ?: ""))
            }
            R.id.verifyStatus -> {
                when (UserInfoBeanHolder.getUserInfoBean()?.verifyStrFlag()) {
                    UserInfoBean.unSubmit,
                    UserInfoBean.checkedYes,
                    UserInfoBean.checkedNot -> {
                        ARouter.getInstance()
                            .build(MineARouter.IDENTIFY_VERIFY)
                            .startNavigation(this)
                    }
                    UserInfoBean.checking->{
                        normalShortToast(_mActivity,"正在实名审核中，请稍后")
                    }
                }

            }
        }

    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when (parent?.id) {
            R.id.mIntegratedApplication -> {
                handleIntegratedApplication(position)
            }
            R.id.orderGridView -> {
                handleOrderGridView(position)
            }
            R.id.mService -> {
                handleService(position)
            }
        }
    }

    private fun handleService(position: Int) {
        val item = mServiceAdapter?.getItem(position)
        if ((item?.path ?: "").isNotEmpty()) {
            ARouter.getInstance()
                .build(item?.path)
                .startNavigation(this@MineFragment)
        }
    }

    private fun handleOrderGridView(position: Int) {
        val item = orderGridViewAdapter?.getItem(position)
        if ((item?.path ?: "").isNotEmpty()) {
            ARouter.getInstance()
                .build(item?.path)
                .withInt(PAGE_INDEX_OF, item?.pageIndexOf ?: -1)
                .startNavigation(this@MineFragment)
        }
    }

    private fun handleIntegratedApplication(position: Int) {
        val item = mGridViewAdapter?.getItem(position)
        if (item?.title == R.string.shou_kuan) {

            when (UserInfoBeanHolder.getUserInfoBean()?.verifyStrFlag()) {
                UserInfoBean.checkedNot,
                UserInfoBean.unSubmit -> {
                    normalShortToast(_mActivity, "请先实名审核")
                    ARouter.getInstance()
                        .build(MineARouter.IDENTIFY_VERIFY)
                        .startNavigation(this@MineFragment)
                }
                UserInfoBean.checking -> {
                    normalShortToast(_mActivity, "正在实名审核中，请稍后")

                }
                else -> {
                    ARouter.getInstance()
                        .build(item.path)
                        .startNavigation(this@MineFragment)
                }
            }


        } else if ((item?.path ?: "").isNotEmpty()) {
            ARouter.getInstance()
                .build(item?.path)
                .startNavigation(this@MineFragment)
        }

    }


}